﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml;
using System.IO;
using He.Common;
using System.Text.RegularExpressions;

namespace He.Web.UI
{
    public class BizHelper
    {
        /// <summary>
        /// 获取系统频道
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Model.channel GetChannel(string name)
        {
            return new BLL.channel().GetModel(name);
        }

        /// <summary>
        /// 获取顶级分类列表
        /// </summary>
        /// <param name="top"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<Model.category> GetCategorys(string name, int top)
        {
            BLL.category bll = new BLL.category();
            return bll.DataTableToList(bll.GetList(top, CombChannelCondition(name) + " and parent_id=0").Tables[0]);
        }

        /// <summary>
        /// 获取所有分类列表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pid"></param>
        /// <returns></returns>
        public List<Model.category> GetCategorysByPid(string name, int pid)
        {
            BLL.category bll = new BLL.category();
            return bll.DataTableToList(bll.GetListByName(pid, name));
        }

        /// <summary>
        /// 获取分类名称
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        public string GetCategoryTitle(int cateId)
        {
            return new BLL.category().GetModel(cateId).title;
        }

        /// <summary>
        /// 根据别名获取article模型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Model.article GetArticle(string name)
        {
            return new BLL.article().GetModel(name);
        }


        /// <summary>
        /// 获取文章模型
        /// </summary>
        /// <param name="name"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public Model.article GetArticle(string name, string strWhere)
        {
            return new BLL.article().GetFristModel(name, strWhere);
        }


        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="top"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Model.article> GetList(string name, int top, string strWhere)
        {
            BLL.article bll = new BLL.article();
            return bll.DataTableToList(bll.GetList(name, top, strWhere, "sort_id asc,id desc").Tables[0]);
        }


        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="top"></param>
        /// <returns></returns>
        public List<Model.article> GetList(string name, int top)
        {
            return GetList(name, top, "");
        }

        /// <summary>
        /// 获取相应分类文章列表
        /// </summary>
        /// <param name="cateId"></param>
        /// <param name="top"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<Model.article> GetList(int cateId, int top, string condition)
        {
            BLL.article bll = new BLL.article();
            return bll.DataTableToList(bll.GetList(cateId, top, condition).Tables[0]);
        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Model.article> GetList(int cateId)
        {
            BLL.article bll = new BLL.article();
            string condition = string.Format("category_id={0} and is_lock=0", cateId);
            return bll.DataTableToList(bll.GetList(condition, "sort_id asc,id desc").Tables[0]);
        }

        /// <summary>
        /// 获取扩展字段
        /// </summary>
        /// <param name="fieldId"></param>
        /// <returns></returns>
        public Model.attr_field GetAttrField(int fieldId)
        {
            return new BLL.attr_field().GetModel(fieldId);
        }

        /// <summary>
        /// 获取扩展字段值
        /// </summary>
        /// <param name="fieldId"></param>
        /// <returns></returns>
        public List<Model.attr_value> GetAttrValueList(string fieldName, string orderBy)
        {
            return new BLL.attr_value().GetModelList(" field_name='" + fieldName + "' ORDER BY " + orderBy);
        }

        /// <summary>
        /// 判断是否为简单文本
        /// </summary>
        /// <param name="control_type"></param>
        /// <returns></returns>
        public bool IsSimpleTxt(string control_type)
        {
            if (control_type == "single-text" || control_type == "number" || control_type == "multi-text")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 将图片地址字符串转换为图片地址集合
        /// </summary>
        /// <param name="imgurls"></param>
        /// <returns></returns>
        public List<string> ImgsToList(string imgurls)
        {
            List<string> lst = new List<string>();
            string[] imgArr = imgurls.Split(',');
            if (imgArr.Length > 0)
            {
                foreach (string item in imgArr)
                {
                    lst.Add(item);
                }
            }
            return lst;
        }



        /// <summary>
        /// 获取滚动图片数量
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public int GetRollCount(string name, int limit)
        {
            int count = new BLL.article().GetRecordCount(string.Format("category_id in(select id from {0}category where {1}) and is_red=1 and is_lock=0", SysKeys.TABLE_PREFIX, CombChannelCondition(name)));
            if (count > limit)
            {
                return limit;
            }
            return count;
        }

        /// <summary>
        /// 获取多行滚动图片换行的步长
        /// </summary>
        /// <param name="count"></param>
        /// <param name="lineCount"></param>
        /// <returns></returns>
        public int GetRollStep(int count, int lineCount)
        {
            if (count % lineCount == 0)
            {
                return count / lineCount;
            }
            else
            {
                return count / lineCount + 1;
            }
        }


        /// <summary>
        /// 获取广告图列表
        /// </summary>
        /// <param name="call_mark"></param>
        /// <returns></returns>
        public List<Model.ad> GetAdList(string call_mark)
        {
            BLL.ad bll = new BLL.ad();
            List<Model.ad> lst = bll.DataTableToList(bll.GetListByMark("is_red=1 and call_mark='" + call_mark + "'").Tables[0]);
            return lst;
        }

        /// <summary>
        /// 获取友情链接
        /// </summary>
        /// <param name="lang"></param>
        /// <returns></returns>
        public List<Model.links> GetLinks(string lang)
        {
            BLL.links bll = new BLL.links();
            return bll.DataTableToList(bll.GetList(string.Format("is_red=1 and lang='{0}' and pos_mark='friend' order by sort_id asc,id desc", lang)).Tables[0]);
        }

        /// <summary>
        /// 获取在线客服列表
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public List<Model.olservice> GetOlServices(int groupId)
        {
            BLL.olservice bll = new BLL.olservice();
            return bll.GetModelList("is_red=1 and group_id=" + groupId);
        }

        /// <summary>
        /// 获取人才招聘列表
        /// </summary>
        /// <param name="lang"></param>
        /// <returns></returns>
        public List<Model.job> GetJobList(string lang, int top)
        {
            BLL.job bll = new BLL.job();
            return bll.DataTableToList(bll.GetList(top, string.Format("lang='{0}'", lang), "sort_id asc,id desc").Tables[0]);
        }


        /// <summary>
        /// 获取文件后缀
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string GetFileExtension(string filePath)
        {
            return Path.GetExtension(filePath).Trim('.');
        }

        /// <summary>
        /// 意见反馈回复内容
        /// </summary>
        /// <param name="recontent"></param>
        /// <returns></returns>
        public string GetReContent(string recontent)
        {
            if (!string.IsNullOrEmpty(recontent))
            {
                return string.Format("<br /><b>回复：{0}</b>", recontent);
            }
            return string.Empty;
        }
        /// <summary>
        /// 意见反馈回复内容
        /// </summary>
        /// <param name="recontent"></param>
        /// <returns></returns>
        public string GetReContentEn(string recontent)
        {
            if (!string.IsNullOrEmpty(recontent))
            {
                return string.Format("<br /><b>Reply：{0}</b>", recontent);
            }
            return string.Empty;
        }

        /// <summary>
        /// 是否为首页
        /// </summary>
        /// <returns></returns>
        public bool IsIndex()
        {
            string page = InputUtil.Get("p", "index");
            if (page == "index")
            {
                return true;
            }
            return false;
        }

        #region 私有方法
        private string CombChannelCondition(string name)
        {
            return string.Format("channel_id in(select id from {0}channel where name='{1}')", SysKeys.TABLE_PREFIX, name);
        }

        #endregion

    }
}
